// SPDX-License-Identifier: GPL-2.0

#include <linux/types.h>
#include <linux/mutex.h>
#include <asm/errno.h>
#include "dim_core_status.h"

static enum dim_core_running_status dim_running_status = DIM_OFF;
DEFINE_MUTEX(dim_running_status_mutex);

static const char *dim_core_running_status_str[] = {
	[DIM_OFF] = "DIM_OFF",
	[DIM_NO_BASELINE] = "DIM_NO_BASELINE",
	[DIM_BASELINE_RUNNING] = "DIM_BASELINE_RUNNING",
	[DIM_MEASURE_RUNNING] = "DIM_MEASURE_RUNNING",
	[DIM_PROTECTED] = "DIM_PROTECTED",
	[DIM_ERROR] = "DIM_ERROR",
	[DIM_STATUS_LAST] = "",
};

void dim_init_running_status(void)
{
	mutex_init(&dim_running_status_mutex);
}

int dim_lock_running_status(void)
{
	return mutex_trylock(&dim_running_status_mutex) ? 0 : -1;
}

void dim_lock_running_status_waiting(void)
{
	mutex_lock(&dim_running_status_mutex);
}

void dim_unlock_running_status(void)
{
	mutex_unlock(&dim_running_status_mutex);
}

int dim_set_running_status(enum dim_core_running_status status)
{
	if (status < 0 || status >= DIM_STATUS_LAST)
		return -EINVAL;

	dim_running_status = status;
	return 0;
}

enum dim_core_running_status dim_get_running_status(void)
{
	return dim_running_status;
}

const char *dim_get_running_status_str(void)
{
	if (dim_running_status < 0 || dim_running_status >= DIM_STATUS_LAST)
		return "";

	return dim_core_running_status_str[dim_running_status];
}
